package com.ltmonitor.web.controller.map;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.MapArea;
import com.ltmonitor.service.IMapAreaService;
import com.ltmonitor.web.controller.GenericAction;
import com.ltmonitor.web.vo.TreeNode;

/**
 * 将地图的地标、区域和关键点 组成一个树菜单
 * 
 * @author DELL
 * 
 */
@Controller
@RequestMapping("/mapTree")
public class MapTreeAction extends GenericAction {

	@Autowired
	private IMapAreaService mapAreaService;


	@RequestMapping("/getMapTree.action")
	@ResponseBody
	public ArrayList<TreeNode> getMapTree() {
		Collection<MapArea> ls = this.getMapAreaList();
		ArrayList<TreeNode> treeNodes = new ArrayList<TreeNode>();
		TreeNode polygonNode = new TreeNode("polygon", "多边形电子围栏", "",
				"icon-polygon");
		polygonNode.setState("open");
		TreeNode rectNode = new TreeNode("rect", "矩形电子围栏", "", "icon-rect");
		rectNode.setState("open");
		TreeNode circleNode = new TreeNode("circle", "圆形电子围栏", "",
				"icon-circle");
		circleNode.setState("open");
		TreeNode keyPointNode = new TreeNode("keyPoint", "关键点", "",
				"icon-keyPoint");
		keyPointNode.setState("open");
		TreeNode routeNode = new TreeNode("route", "路线", "", "icon-route");
		routeNode.setState("open");
		TreeNode markerNode = new TreeNode("marker", "地图标注", "", "icon-marker");
		markerNode.setState("open");
		for (MapArea ec : ls) {
			TreeNode tn = null;
			if (ec.getKeyPoint() == 1) {
				tn = new TreeNode("" + ec.getEntityId(), ec.getName(),
						"keyPoint");
				tn.setChildren(null);
				tn.setLeaf(true);
				keyPointNode.getChildren().add(tn);
			} else {
				if (MapArea.POLYGON.equals(ec.getAreaType())) {
					tn = new TreeNode("" + ec.getEntityId(), ec.getName(),
							"polygon");
					tn.setChildren(null);
					tn.setLeaf(true);
					polygonNode.getChildren().add(tn);
				} else if (MapArea.RECT.equals(ec.getAreaType())) {
					tn = new TreeNode("" + ec.getEntityId(), ec.getName(),
							"rect");
					tn.setChildren(null);
					tn.setLeaf(true);
					rectNode.getChildren().add(tn);
				} else if (MapArea.CIRCLE.equals(ec.getAreaType())) {
					tn = new TreeNode("" + ec.getEntityId(), ec.getName(),
							"circle");
					tn.setChildren(null);
					circleNode.getChildren().add(tn);
				} else if (MapArea.ROUTE.equals(ec.getAreaType())) {
					tn = new TreeNode("" + ec.getEntityId(), ec.getName(),
							"route");
					tn.setChildren(null);
					tn.setLeaf(true);
					routeNode.getChildren().add(tn);
				} else if (MapArea.MARKER.equals(ec.getAreaType())) {
					tn = new TreeNode("" + ec.getEntityId(), ec.getName(),
							"marker");
					tn.setChildren(null);
					tn.setLeaf(true);
					markerNode.getChildren().add(tn);
				}
			}

			if (tn != null) {
				tn.getAttributes().put("lat", ec.getCenterLat());
				tn.getAttributes().put("lng", ec.getCenterLng());
				tn.setIconCls("icon-area");
				tn.setChildren(null);
				tn.setState("open");
				tn.setLeaf(true);
			}
		}

		if (polygonNode.getChildren().size() > 0)
			treeNodes.add(polygonNode);
		if (rectNode.getChildren().size() > 0)
			treeNodes.add(rectNode);
		if (circleNode.getChildren().size() > 0)
			treeNodes.add(circleNode);
		if (keyPointNode.getChildren().size() > 0)
			treeNodes.add(keyPointNode);
		if (routeNode.getChildren().size() > 0)
			treeNodes.add(routeNode);
		if (markerNode.getChildren().size() > 0)
			treeNodes.add(markerNode);

		return treeNodes;
	}


	private Collection<MapArea> getMapAreaList() {
		if (this.getOnlineUser().isSuperAdmin()) {
			//超级用户能看到所有区域
			String hsql = "from MapArea where  deleted = 0";
			return this.mapAreaService.loadAll();
		} else {
			//根据用户所属的部门，查询区域
			List<Long> parentIdList = this.getDepartmentService()
					.getParentDepIdList(this.getOnlineUser().getCompanyId());
			parentIdList.add(0L);
			String hql = "from MapArea where depId in (:depIdList) and deleted = 0";

			List<MapArea> ls = this.mapAreaService.getMapAreaList(parentIdList);
			return ls;
		}
	}
}
